package com.guigu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.VendorService;
import com.guigu.ZhucheService;
import com.guigu.pojo.Businesses;
import com.guigu.pojo.FrontUser;
import com.guigu.pojo.FrontUser2;
import com.guigu.pojo.Vendor;
import com.guigu.vo.Pojo;
import com.guigu.vo.VendorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/vendor")
@CrossOrigin
@RestController
public class VendorController {
    @Autowired
    private VendorService vendorService;
    @Autowired
    private ZhucheService zhucheService;
    //根据userid查询
    @RequestMapping("/selectById")
    public Vendor selectById(int userId) {
        System.out.println("vendor"+userId);
        QueryWrapper<Vendor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Vendor vendor = vendorService.getOne(queryWrapper);
        System.out.println(vendor);
        return vendor;
    }
    //审核分页查询
    @PostMapping("/select")
    public Pojo<FrontUser2> select(@RequestBody VendorVo vo, Integer pageNum, Integer pageSize){
        System.out.println(vo);
        return vendorService.getPage(pageNum,pageSize,vo);
    }
    //根据id查询
    @RequestMapping("/selectById1")
    public Vendor selectById1(int id) {
        System.out.println("vendor"+id);
        Vendor frontUser2 = vendorService.getById(id);
        System.out.println(frontUser2);
        return frontUser2;
    }
    //根据id删除
    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable int id){
        System.out.println("vendor"+id);
        boolean b = vendorService.removeById(id);
        if (b)
            return "ok";
        return "no";
    }
    //根据id修改
    @PutMapping("/updateById")
    public String updateById(@RequestBody Vendor vendor) {
        System.out.println("vendor"+vendor);
        boolean b = vendorService.updateById(vendor);
        if (b)
            return "ok";
        return "no";
    }
    //根据userId删除
    @DeleteMapping("/deleteById6/{userId}")
    public String deleteById1(@PathVariable int userId) {
        System.out.println("vendor"+userId);
        QueryWrapper<Vendor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        boolean b = vendorService.remove(queryWrapper);
        if (b)
            return "ok";
        return "no";
    }
    //liyongchao

    @PostMapping("insert")
    public String insert(@RequestBody Vendor vendor) {
        vendor.setCheckTag("S001-0");
        boolean b = vendorService.save(vendor);
        if (b) {
            int userId = vendor.getUserId();
            FrontUser user = zhucheService.One(userId);
            user.setIsVendor(0);
            boolean b1 = zhucheService.updateById(user);
            if (b1)
                return "ok";
            return "no";
        }
        return "no";
    }

    @PostMapping("{id}")
    public Vendor One(@PathVariable int id) {
        Vendor one = vendorService.One(id);
        return one;
    }

    @PutMapping("update")
    public String update(@RequestBody  Vendor vendor) {
        boolean b = vendorService.updateById(vendor);
        if (b)
            return "ok";
        return "no";
    }
    //根据userId查询
    @PostMapping("selectOne/{userId}")
    public Vendor selectOne(@PathVariable int userId) {
        Vendor one = vendorService.One1(userId);
        return one;
    }

}
